﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;

namespace Recipes.Views
{
    /// <summary>
    /// This scenario reads from a view.
    /// </summary>
    [TestCategory("Views")]
    public abstract class ViewsTests<TEmployeeDetail, TEmployeeSimple> : TestBase
       where TEmployeeDetail : class, IEmployeeDetail
       where TEmployeeSimple : class, IEmployeeSimple, new()
    {
        [TestMethod]
        public void FindByEmployeeClassificationKey()
        {
            var repository = GetScenario();

            var employeeClassification = repository.GetClassification(3);
            Assert.IsNotNull(employeeClassification);

            var newRecord = new TEmployeeSimple
            {
                FirstName = "Test",
                MiddleName = "A",
                LastName = "Person " + DateTime.Now.Ticks,
                EmployeeClassificationKey = employeeClassification!.EmployeeClassificationKey
            };
            var newKey = repository.Create(newRecord);
            Assert.IsTrue(newKey >= 1000, "keys under 1000 were not generated by the database");

            var list = repository.FindByEmployeeClassificationKey(employeeClassification!.EmployeeClassificationKey);
            var echo = list.Single(x => x.EmployeeKey == newKey);
            Assert.IsNotNull(echo);
            Assert.AreEqual(newKey, echo.EmployeeKey);
            Assert.AreEqual(newRecord.FirstName, echo.FirstName);
            Assert.AreEqual(newRecord.MiddleName, echo.MiddleName);
            Assert.AreEqual(newRecord.LastName, echo.LastName);
            Assert.AreEqual(newRecord.EmployeeClassificationKey, echo.EmployeeClassificationKey);
            Assert.AreEqual(employeeClassification.EmployeeClassificationName, echo.EmployeeClassificationName);
            Assert.AreEqual(employeeClassification.IsEmployee, echo.IsEmployee);
            Assert.AreEqual(employeeClassification.IsExempt, echo.IsExempt);
        }

        [TestMethod]
        public void FindByName()
        {
            var repository = GetScenario();

            var employeeClassification = repository.GetClassification(3);
            Assert.IsNotNull(employeeClassification);

            var newRecord = new TEmployeeSimple
            {
                FirstName = "Test",
                MiddleName = "A",
                LastName = "Person " + DateTime.Now.Ticks,
                EmployeeClassificationKey = employeeClassification!.EmployeeClassificationKey
            };
            var newKey = repository.Create(newRecord);
            Assert.IsTrue(newKey >= 1000, "keys under 1000 were not generated by the database");

            var list = repository.FindByLastName(newRecord.LastName);
            var echo = list.Single();
            Assert.IsNotNull(echo);
            Assert.AreEqual(newKey, echo.EmployeeKey);
            Assert.AreEqual(newRecord.FirstName, echo.FirstName);
            Assert.AreEqual(newRecord.MiddleName, echo.MiddleName);
            Assert.AreEqual(newRecord.LastName, echo.LastName);
            Assert.AreEqual(newRecord.EmployeeClassificationKey, echo.EmployeeClassificationKey);
            Assert.AreEqual(employeeClassification.EmployeeClassificationName, echo.EmployeeClassificationName);
            Assert.AreEqual(employeeClassification.IsEmployee, echo.IsEmployee);
            Assert.AreEqual(employeeClassification.IsExempt, echo.IsExempt);
        }

        [TestMethod]
        public void GetByEmployeeKey()
        {
            var repository = GetScenario();

            var employeeClassification = repository.GetClassification(3);
            Assert.IsNotNull(employeeClassification);

            var newRecord = new TEmployeeSimple
            {
                FirstName = "Test",
                MiddleName = "A",
                LastName = "Person " + DateTime.Now.Ticks,
                EmployeeClassificationKey = employeeClassification!.EmployeeClassificationKey
            };
            var newKey = repository.Create(newRecord);
            Assert.IsTrue(newKey >= 1000, "keys under 1000 were not generated by the database");

            var echo = repository.GetByEmployeeKey(newKey);
            Assert.IsNotNull(echo);
            Assert.AreEqual(newKey, echo!.EmployeeKey);
            Assert.AreEqual(newRecord.FirstName, echo.FirstName);
            Assert.AreEqual(newRecord.MiddleName, echo.MiddleName);
            Assert.AreEqual(newRecord.LastName, echo.LastName);
            Assert.AreEqual(newRecord.EmployeeClassificationKey, echo.EmployeeClassificationKey);
            Assert.AreEqual(employeeClassification.EmployeeClassificationName, echo.EmployeeClassificationName);
            Assert.AreEqual(employeeClassification.IsEmployee, echo.IsEmployee);
            Assert.AreEqual(employeeClassification.IsExempt, echo.IsExempt);
        }

        protected abstract IViewsScenario<TEmployeeDetail, TEmployeeSimple> GetScenario();
    }
}
